﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace ProjectCodeS.Models
{
    //public class WebsiteInitializer : DropCreateDatabaseIfModelChanges<ProjectCodeSContext>
    public class WebsiteInitializer : DropCreateDatabaseAlways<ProjectCodeSContext>
    {
        protected override void Seed(ProjectCodeSContext context)
        {
          
            var companies = new List<Company>
            {
                new Company{ Name = "Company1", Street1="Company 1 Street 1", City="City1", State="FL", ZipCode=33333,Country="US",PhoneNumber1=1234},
                new Company{ Name = "Company2", Street1="Company 2 Street 2", City="City2", State="FL", ZipCode=33333,Country="US",PhoneNumber1=1234}
            };
            companies.ForEach(c => context.Companies.Add(c));
            context.SaveChanges();

            var users = new List<User>
            {
                new User { Username = "Admin", UserEmailAddress="admin@temp.com", DateCreated = DateTime.Now, LastUpdated = DateTime.Now, CompanyId = companies[0].Id},
                new User { Username = "Member", UserEmailAddress="member@temp.com",DateCreated = DateTime.Now, LastUpdated = DateTime.Now, CompanyId = companies[0].Id}
            };
            users.ForEach(u => u.Password = HashPassword("password"));
            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            var userroles = new List<UserRole>
            {
                new UserRole { Name ="Administrator", DateCreated=DateTime.Now, LastUpdated=DateTime.Now, Users = new List<User>()},
                new UserRole { Name ="Member", DateCreated = DateTime.Now, LastUpdated = DateTime.Now, Users = new List<User>()}
            };
            userroles.ForEach(r => context.UserRoles.Add(r));
            context.SaveChanges();

            userroles[0].Users.Add(users[0]);
            userroles[1].Users.Add(users[0]);
            userroles[1].Users.Add(users[1]);
            context.SaveChanges();

            var servicetypes = new List<ServiceType>
            {
                new ServiceType{ Name = "ServiceType1"},
                new ServiceType{ Name = "ServiceType2"},
                new ServiceType{ Name = "ServiceType3"},
                new ServiceType{ Name = "ServiceType4"},
                new ServiceType{ Name = "ServiceType5"}
            };
            servicetypes.ForEach(s => context.ServiceTypes.Add(s));
            context.SaveChanges();

            var chargecategories = new List<ChargeCategory>
            {
                new ChargeCategory{ Name = "ChargeCategory1", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory2", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory3", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory4", ServiceTypes= new List<ServiceType>()},
            };
            chargecategories.ForEach(c => context.ChargeCategories.Add(c));
            context.SaveChanges();

            chargecategories[0].ServiceTypes.Add(servicetypes[0]);
            chargecategories[0].ServiceTypes.Add(servicetypes[1]);
            chargecategories[0].ServiceTypes.Add(servicetypes[2]);
            chargecategories[1].ServiceTypes.Add(servicetypes[3]);
            chargecategories[2].ServiceTypes.Add(servicetypes[4]);
            chargecategories[3].ServiceTypes.Add(servicetypes[4]);
            context.SaveChanges();

            var stores = new List<Store>
            {
                new Store { Name = "Store1", CompanyId = companies[0].Id, ChargeCategoryId = chargecategories[0].Id},
                new Store { Name = "Store2", CompanyId = companies[0].Id, ChargeCategoryId = chargecategories[1].Id},
                new Store { Name = "Store3", CompanyId = companies[0].Id, ChargeCategoryId = chargecategories[2].Id},
                new Store { Name = "Store4", CompanyId = companies[0].Id, ChargeCategoryId = chargecategories[3].Id},
            };
            stores.ForEach(s => context.Stores.Add(s));
            context.SaveChanges();

            var items = new List<Item>();
            for (int i = 0; i < 30; i++)
            {
                items.Add(new Item{ Name = "Item"+(i+1), ServiceTypeId = servicetypes[i%5].Id, Price=((i%5)+1.50)});
            }

            items.ForEach(i => context.Items.Add(i));
            context.SaveChanges();

            var clients = new List<Client>
            {
                new Client{LastAccessed=DateTime.Now, Created = DateTime.Now, LastEdited = DateTime.Now, LastName="LastNameC1", Phone="0123456789", FirstName = "FirstNameC1", StoreId = stores[0].Id },
                new Client{LastAccessed=DateTime.Now, Created = DateTime.Now, LastEdited = DateTime.Now, LastName="LastNameC2", Phone="0123456789", FirstName = "FirstNameC2", StoreId = stores[1].Id },
            };

            clients.ForEach(c => context.Clients.Add(c));
            context.SaveChanges();
        }
        private string HashPassword(string password)
        {
            int workfactor = 12;
            return BCrypt.Net.BCrypt.HashPassword(password, workfactor);
        }
    }
}